package com.zego.wawaji.ui.rank.prize;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.noble.library.basic.log.PLogger;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.rank.bean.Award;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Su on 2018/1/9.
 */
public class PrizeAdapter extends ExBaseAdapter<Award> {
    private List<Award> chooseList = new ArrayList<>();   // 选中的奖品

    public PrizeAdapter(Context context, List<Award> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.my_prize_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final Award info = getItem(position);

        ImageLoader.loadRoundAvatar(getContext(), info.getAwardImg(), holder.price_icon);
        holder.wawa_name.setText(info.getAwardName());

        holder.iv_choose.setVisibility(View.INVISIBLE);
        if (info.getAwardType() == 1) {
            holder.iv_choose.setVisibility(View.VISIBLE);
            holder.iv_choose.setSelected(info.isChoose());
        }

        String remark = info.getAwardRemark();
        holder.describe_info.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(remark)) {
            holder.describe_info.setVisibility(View.VISIBLE);
            holder.describe_info.setText(remark);
        }

        holder.rl_item.setOnClickListener(v -> {
            try {
                if (info.getAwardType() == 1) {    // 实物奖品
                    if (holder.iv_choose.isSelected()) {
                        holder.iv_choose.setSelected(false);
                        if (chooseList.contains(info)) {
                            chooseList.remove(info);
                        }
                        info.setChoose(false);
                    } else {
                        holder.iv_choose.setSelected(true);
                        chooseList.add(info);
                        info.setChoose(true);
                    }
                } else if (info.getAwardType() == 2) {  // 虚拟奖品
                    UIShow.showVirtualPrizeActivity((Activity) getContext(), info);
                }
            } catch (Exception e) {
                PLogger.printThrowable(e);
            }
        });
        return convertView;
    }

    public List<Award> getChooseList() {
        return chooseList;
    }

    public void reset() {
        if (chooseList != null)
            chooseList.clear();
    }

    private class MyViewHolder {
        private ImageView price_icon, iv_choose;
        private RelativeLayout rl_item;


        private TextView wawa_name, describe_info;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            price_icon = (ImageView) convertView.findViewById(R.id.price_icon);
            wawa_name = (TextView) convertView.findViewById(R.id.wawa_name);
            describe_info = (TextView) convertView.findViewById(R.id.describe_info);
        }
    }
}

